package com.dragon.www.smarthome.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dragon.www.smarthome.R;
import com.dragon.www.smarthome.entity.LoginInfo;
import com.dragon.www.smarthome.entity.ServiceResult;
import com.dragon.www.smarthome.entity.User;
import com.dragon.www.smarthome.net.NetHelper;
import com.dragon.www.smarthome.net.NetUtils;
import com.dragon.www.smarthome.util.Config;

/**
 * Created by daiye on 2016/5/31.
 */
public class LoginActivity extends FragmentActivity {

    private final int ACTIVITY_RESULT_REGISTER = 1;
    private EditText mMailEt = null;
    private EditText mPasswordEt = null;
    private Button mLoginBt = null;
    private Button mRegisterBt = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initData(){

    }

    private void initView() {
        setContentView(R.layout.activity_login);
        mMailEt = (EditText)findViewById(R.id.mail_et);
        mPasswordEt = (EditText)findViewById(R.id.password_et);
        mLoginBt = (Button)findViewById(R.id.login_bt);
        mLoginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
        mRegisterBt = (Button)findViewById(R.id.register_user_bt);
        mRegisterBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent toIntent1 = new Intent(LoginActivity.this, RegisterActivity.class);
                toIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivityForResult(toIntent1, ACTIVITY_RESULT_REGISTER);
            }
        });
    }

    private void submit(){
        final String mailStr = mMailEt.getText().toString();
        String passwordStr = mPasswordEt.getText().toString();
        if(TextUtils.isEmpty(mailStr) ){
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(passwordStr) ){
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(passwordStr.length() < 6 ){
            Toast.makeText(this, "密码长度不能低于6位", Toast.LENGTH_SHORT).show();
            return;
        }

        // 用户登录请求
            NetHelper.userLogin(this, mailStr, passwordStr, new NetUtils.NetCallBack<ServiceResult>() {
                @Override
                public void success(ServiceResult rspData) {
                    if (rspData.getCode().equals("0000")) {
                        //保存用户信息
                        User user = new User();
                        user.setUserName(mailStr);
                        Config.setUserInfo(user);
                        Intent intent = new Intent();
                        setResult(RESULT_OK, intent);
                        LoginActivity.this.finish();
                    }else{
                        Toast.makeText(LoginActivity.this, "登录操作失败，原因"+rspData.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }

            @Override
            public void failed(String msg) {
                Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        }, LoginInfo.class);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ACTIVITY_RESULT_REGISTER) {
            if(resultCode == RESULT_OK){

            }
        }
    }
}
